package com.order.controller.buyer;


import com.order.annotation.LoggerManage;
import com.order.converter.OrderForm2OrderDTOConverter;
import com.order.dto.OrderDTO;
import com.order.dto.OrderDetailDTO;
import com.order.form.OrderForm;
import com.order.pojo.ProductCategory;
import com.order.service.IOrderMasterService;
import com.order.service.PushMessageService;
import com.order.utils.Result;
import com.order.vo.ResultVO;
import com.order.websocket.WebSocket;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "买家订单接口")
@RestController
@ApiResponses({
        @ApiResponse(code = 200, message = "操作成功", response = ProductCategory.class),
        @ApiResponse(code = 400, message = "参数错误"),
        @ApiResponse(code = 403, message = "权限被禁止"),
        @ApiResponse(code = 500, message = "服务器内部错误")
})
@RequestMapping("/sell/buyer")
@Slf4j
public class BuyerOrderController {

    @Autowired
    IOrderMasterService orderMasterService;

    @Autowired
    PushMessageService pushMessageService;

    @Autowired
    WebSocket webSocket;

    @LoggerManage("买家下单")
    @ApiOperation(value = "买家下单", notes = "买家下单....")
    @PostMapping("/order/create")
    public ResultVO<OrderDTO> createOrder(OrderForm orderForm){
        if(orderForm.getOpenid() == null || orderForm.getOpenid().isEmpty()){
            return new ResultVO<>(1,"未登录",null);
        }
        OrderDTO orderDTO = OrderForm2OrderDTOConverter.convert(orderForm);
        log.info("买家下单的数据："+orderDTO);
        //调用下单业务
        OrderDTO order=orderMasterService.createOrder(orderDTO);
        //发送模板消息
        pushMessageService.orderStatus(order.getOrderId());
        log.info("买家下单的数据,进行消息推送："+orderDTO);
        //给卖家推动新的订单消息
        webSocket.sendMessage(order.getOrderId());
        return new ResultVO<>(0,"成功",order);
    }

    @LoggerManage("买家查看订单")
    @ApiOperation(value = "买家查看订单", notes = "买家查看订单....")
    @GetMapping("/order/list")
    public ResultVO<List<OrderDTO>> getOrderList(String openid){
        if(openid == null || openid.isEmpty()){
            return new ResultVO<>(1,"未登录",null);
        }
        List<OrderDTO> orderDTOList = orderMasterService.getOrderList(openid);
        return new ResultVO<>(0,"成功",orderDTOList);
    }

    @LoggerManage("买家查看订单详情")
    @ApiOperation(value = "买家查看订单详情", notes = "买家查看订单详情....")
    @GetMapping("/order/detail")
    public ResultVO<OrderDetailDTO> getOrderDetail(String orderId, String openid){
        if(openid == null || openid.isEmpty()){
            return new ResultVO<>(1,"未登录",null);
        }
        OrderDetailDTO orderDetailDTO = orderMasterService.getOrderDetail(orderId,openid);
        return new ResultVO<>(0,"成功",orderDetailDTO);
    }

    @LoggerManage("买家取消订单")
    @ApiOperation(value = "买家取消订单")
    @PostMapping("/order/cancel/")
    public ResultVO<String> cancelOrder( String orderId, String openid) {
        if (openid == null || openid.isEmpty()) {
            return new ResultVO<>(1, "未登录", null);
        }
        try{
            if (orderMasterService.cancelOrder(orderId, openid)) {
                //发送模板消息
                pushMessageService.orderStatus(orderId);
                return new ResultVO<>(0, "订单取消成功", null);
            } else {
                return new ResultVO<>(1, "订单取消失败", null);
            }
        }catch (Exception e){
            return new ResultVO<>(1, "订单取消失败", null);
        }
    }
}
